<?php

if (!defined('FRAMEWORKS_RB_FUNCTIONS_USER')):
define('FRAMEWORKS_RB_FUNCTIONS_USER', true);

function rb_check_perm($perm_itemid, $perm_type)
{
	global $xoopsUser, $xoopsModule;	
	if ($xoopsUser) {
		$groups = $xoopsUser->getGroups();
	} else {
		$groups = XOOPS_GROUP_ANONYMOUS;
	}
		
	$module_id = $xoopsModule->getVar('mid');
	$gperm_handler =& xoops_gethandler('groupperm');
	if ($gperm_handler->checkRight($perm_type, $perm_itemid, $groups, $module_id)) {
		return true;
	}
	return false;
}



// 解析真實IP
function rb_get_ip()
{
    static $realip = NULL;

    if ($realip !== NULL) {
        return $realip;
    }

    if (isset($_SERVER)) {
        if (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
			if(!preg_match("/^(10|172\.16|192\.168)\./i", $ip)) {
				$realip = $ip;
			} else {
				$realip = $_SERVER['REMOTE_ADDR'];
			}
		} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            foreach ($arr AS $ip)
            {
                $ip = trim($ip);
                if ($ip != 'unknown') {
                    if(!preg_match("/^(10|172\.16|192\.168)\./i", $ip)) {
						$realip = $ip;
					} else {
						$realip = $_SERVER['REMOTE_ADDR'];
					}
                    break;
                }
            }
        } else {
            if (isset($_SERVER['REMOTE_ADDR'])) {
                $realip = $_SERVER['REMOTE_ADDR'];
            } else {
                $realip = '0.0.0.0';
            }
        }
    } else {
        if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
			if(!preg_match("/^(10|172\.16|192\.168)\./i", $ip)) {
				$realip = $ip;
			} else {
				$realip = getenv('REMOTE_ADDR');
			}
		} elseif (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
			if(!preg_match("/^(10|172\.16|192\.168)\./i", $ip)) {
				$realip = $ip;
			} else {
				$realip = getenv('REMOTE_ADDR');
			}
        } else {
            $realip = getenv('REMOTE_ADDR');
        }
    }
    preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
    $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
    return $realip;
}

endif;
?>